package com.btex.app.treaty;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.btex.app.BaseActivity;
import com.btex.app.Config;
import com.btex.app.MyApplication;
import com.btex.app.R;
import com.btex.app.entity.ChengJiaoInfo;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.SharedPreferencesUtil;
import com.btex.app.treaty.adapter.ChengJiaoAdapter;
import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChengJiaoActivity extends BaseActivity {


    @BindView(R.id.chengjiao_recy)
    RecyclerView mChengjiaoRecy;
    @BindView(R.id.activity_counter_imag_back)
    ImageView mActivityCounterImagBack;
    @BindView(R.id.activity_history_swip)
    SwipeRefreshLayout mActivityHistorySwip;
    private ChengJiaoAdapter mChengJiaoAdapter;
    private String mNumbers = "8";
    private String pricenumber = "4";
    private String mContract_value = "1";
    private String mIsreverse = "";
    private String mSettle_anchor = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chengjiao);
        ButterKnife.bind(this);

        final String order_id = getIntent().getStringExtra("order_id");
        mNumbers = getIntent().getStringExtra("numbers");
        pricenumber = getIntent().getStringExtra("pricenumber");
        mContract_value = getIntent().getStringExtra("mContract_value");
        mIsreverse = getIntent().getStringExtra("mIsreverse");
        mSettle_anchor = getIntent().getStringExtra("mSettle_anchor");

        mActivityHistorySwip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getMxCcontracts(order_id);
            }
        });

        initRecy();


        getMxCcontracts(order_id);
    }

    private void initRecy() {
        mChengjiaoRecy.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        mChengJiaoAdapter = new ChengJiaoAdapter();
        mChengjiaoRecy.setAdapter(mChengJiaoAdapter);
    }


    @Override
    public void setData() {

    }

    @Override
    public void onClick(View v) {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    private void getMxCcontracts(String order_id) {

        if (mActivityHistorySwip != null) {
            mActivityHistorySwip.setRefreshing(true);
        }
        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(this);
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(this, sharedPreferences);
        RequestParams params = new RequestParams();
        params.addHeader("app-version", Config.VERSION);
        params.addHeader("channel", MyApplication.mApp.getApplicationMetaData("CXD_CHANNEL_ID"));
        params.addHeader("my-client", "android");
        params.addQueryStringParameter("order_id", order_id);

        int num = (int) ((Math.random() * 9 + 1) * 100000);

        MyApplication.get().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.ORDER_MATCH_RESULTS + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Log.i("eruheywfhyw", responseInfo.result);
                if (mActivityHistorySwip != null) {
                    mActivityHistorySwip.setRefreshing(false);
                }

                try {

                    JSONObject jsonObj = new JSONObject(responseInfo.result);
                    if (jsonObj.getString("code").equals("1")) {

                        setList(responseInfo.result);

//                        FuturesListInfo futuresInfo = gson.fromJson(responseInfo.result, FuturesListInfo.class);


                    } else {

                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }


            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }

        });

    }

    private void setList(String result) {

        Gson gson = new Gson();
        ChengJiaoInfo chengJiaoInfo = gson.fromJson(result, ChengJiaoInfo.class);
        List<ChengJiaoInfo.DataBean> data = chengJiaoInfo.getData();
        mChengJiaoAdapter.setNumber(mNumbers);
        mChengJiaoAdapter.setPriceNumber(pricenumber);
        mChengJiaoAdapter.setmContract_value(mContract_value);
        mChengJiaoAdapter.setIszheng(mIsreverse);
        mChengJiaoAdapter.setmSettleanchor(mSettle_anchor);
        mChengJiaoAdapter.setNewData(data);

    }


    @OnClick(R.id.activity_counter_imag_back)
    public void onViewClicked() {
        finish();
    }
}
